import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class Service {
    //按照时间排序
    public void sortLost(Lost[] lostArray) {
        //背了很久的递归，终于可以用一下了
        process(lostArray, 0, lostArray.length - 1);
        for (int i = 0; i < lostArray.length; i++) {
            lostArray[i].toString();
        }
    }

    public static void process(Lost[] nums, int left, int right) {
        if (left == right)
            return;
        int middle = (left + right) >> 1;
        //不断二分
        process(nums, left, middle);
        process(nums, middle + 1, right);
        //往上递归
        merge(nums, left, middle, right);
    }

    public static void merge(Lost[] nums, int l, int m, int r) {
        int i = 0;
        Lost[] arr = new Lost[r - l + 1];
        int leftPoint = l;
        int rightPoint = m + 1;
        while (leftPoint <= m && rightPoint <= r) {
            arr[i++] = nums[rightPoint].getLostDate().getTime() <= nums[leftPoint].getLostDate().getTime() ? nums[rightPoint++] : nums[leftPoint++];
        }
        while (rightPoint <= r) {
            arr[i++] = nums[rightPoint++];
        }
        while (leftPoint <= m) {
            arr[i++] = nums[leftPoint++];
        }
        for (i = 0; i < arr.length; i++) {
            nums[l + i] = arr[i];
        }
    }

    public Lost[] selectByKeyWord(Lost[] lostArray, String keyWord) {
        ArrayList<Lost> arrayList = new ArrayList<>();
        //temp的索引：
        int j = 0;
        for (int i = 0; i < lostArray.length; i++) {
            if (keyWord != null && lostArray[i].getWhereToGet().indexOf(keyWord) > 0) {
                arrayList.add(lostArray[i]);
            }
        }
        //用比较器
        Collections.sort(arrayList);
        //转换
        Lost[] lost = new Lost[arrayList.size()];
        return arrayList.toArray(lost);
    }
}

